package com.systemCapacity.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.systemCapacity.common.dto.BlueEquipmentDto;
import com.systemCapacity.entity.Attribute;
import com.systemCapacity.entity.BattlePlatformDevice;
import com.systemCapacity.entity.Device;
import com.systemCapacity.mapper.AttributeMapper;
import com.systemCapacity.mapper.BattlePlatformDeviceMapper;
import com.systemCapacity.mapper.DeviceMapper;
import com.systemCapacity.service.DeviceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.stream.Collectors;

@Service
public class DeviceServiceImpl extends ServiceImpl<DeviceMapper,Device> implements DeviceService {
    @Autowired
    AttributeMapper attributeMapper;

    @Autowired
    BattlePlatformDeviceMapper battlePlatformDeviceMapper;

    @Override
    @Transactional
    public void transferDate(){
        QueryWrapper<Attribute> attributeQueryWrapper = new QueryWrapper<>();
        List<Attribute> attributeList = attributeMapper.selectList(attributeQueryWrapper);
        Set<String> set = attributeList.stream().map(x -> x.getEquipmentName()).collect(Collectors.toSet());
        List<Device> deviceList = new ArrayList<>();
        deviceList = set.stream().map(x -> {
            Device d = new Device();
            d.setDeviceName(x);
            d.setCreateDate(new Date());
            d.setUpdateDate(new Date());
            d.setDeviceId(UUID.randomUUID().toString());
            return d;
        }).collect(Collectors.toList());
        saveBatch(deviceList);
    }



    @Override
    @Transactional
    public void changeDeviceId(){
        List<Device> deviceList = list();
        Map<String, String> idMap = deviceList.stream().collect(Collectors.toMap(x -> x.getDeviceName(), x -> x.getDeviceId()));

        QueryWrapper<Attribute> attributeQueryWrapper = new QueryWrapper<>();
        List<Attribute> attributeList = attributeMapper.selectList(attributeQueryWrapper);
        for (Attribute attribute : attributeList) {
            String deviceId = idMap.get(attribute.getEquipmentName());
            UpdateWrapper<Attribute> updateWrapper = new UpdateWrapper<>();
            updateWrapper.eq("attributeId",attribute.getAttributeId());
            updateWrapper.set("deviceId",deviceId);
            attributeMapper.update(null,updateWrapper);
        }
    }

    @Override
    @Transactional
    public void changePlatformDeviceId(){
        List<Device> deviceList = list();
        Map<String, String> idMap = deviceList.stream().collect(Collectors.toMap(x -> x.getDeviceName(), x -> x.getDeviceId()));

        QueryWrapper<BattlePlatformDevice> attributeQueryWrapper = new QueryWrapper<>();
//        List<BattlePlatformDevice> battlePlatformDeviceList = battlePlatformDeviceService.list(attributeQueryWrapper);
//        for (BattlePlatformDevice battlePlatformDevice : battlePlatformDeviceList) {
//            String deviceId = idMap.get(battlePlatformDevice.getDeviceName());
//            UpdateWrapper<BattlePlatformDevice> updateWrapper = new UpdateWrapper<>();
//            updateWrapper.eq("id",battlePlatformDevice.getId());
//            updateWrapper.set("deviceId",deviceId);
//            battlePlatformDeviceService.update(null,updateWrapper);
//        }
    }

    @Override
    public Device saveIfNotExists(BlueEquipmentDto dto){
        //判断设备名是否已经存在，存在则不添加
        if(!StringUtils.isEmpty(dto.getEquipmentName())){
            Device deviceByName = queryDeviceByName(dto.getEquipmentName());
            if(deviceByName == null){
                Device device = new Device();
                device.setDeviceId(UUID.randomUUID().toString());
                device.setWeight(dto.getWeight());
                device.setDeviceName(dto.getEquipmentName());
                device.setUpdateDate(new Date());
                device.setCreateDate(new Date());
                save(device);
                return device;
            }else{
                deviceByName.setWeight(dto.getWeight());
                deviceByName.setUpdateDate(new Date());
                updateById(deviceByName);
            }
            return deviceByName;
        }
        return null;

    }

    @Override
    public boolean isExistName(String deviceName){
        QueryWrapper<Device> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("deviceName",deviceName);
        List<Device> list = list(queryWrapper);
        return !list.isEmpty();
    }

    @Override
    public Device queryDeviceByName(String deviceName){
        QueryWrapper<Device> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("deviceName",deviceName);
        return getOne(queryWrapper);
    }

    /**
     * 判断设备是否在平台设备表和参数表中存在，不存在则删除
     * @param deviceId
     * @return
     */
    @Override
    public boolean deleteIfNotExists(String deviceId){
        QueryWrapper<BattlePlatformDevice> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("deviceId",deviceId);
        List<BattlePlatformDevice> battlePlatformDeviceList = battlePlatformDeviceMapper.selectList(queryWrapper);

        QueryWrapper<Attribute> attributeQueryWrapper = new QueryWrapper<>();
        attributeQueryWrapper.eq("deviceId",deviceId);
        List<Attribute> attributeList = attributeMapper.selectList(attributeQueryWrapper);
        if(battlePlatformDeviceList.isEmpty() && attributeList.isEmpty()){
            QueryWrapper<Device> deleteWrapper = new QueryWrapper<>();
            deleteWrapper.eq("deviceId",deviceId);
            remove(deleteWrapper);
            return true;
        }
        return false;
    }

}
